package gui;

import gui.nodes.AbstractNode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;

import utility.Record;
import business.EnseignantBL;
import business.GroupeBL;
import business.ServiceBL;
import business.TypeHeureBL;

public class ServiceUIConsulter extends AbstractViewPane {

	private JComboBox comboProf;
	private JComboBox comboGroupe;
	private JComboBox comboTE;
	private JSpinner spinNbPeriode;
	
	private int id = -1;
	private int idSee;
	private int idTHeure = 1;
	private int idGroupe = 1;
	private int idProf = 1;
	private int nbPeriode;

	private ComponentList componentList = new ComponentList();
	private ViewMode currentViewMode;
	private AbstractNode creator;
	
	public ServiceUIConsulter(AbstractNode creator, ViewMode v, int idService, int see) {
		this.creator = creator;
		currentViewMode = v;
		id = idService;
		idSee = see;
		initGUI(id);
	}

	private JComboBox makeComboTEns (TypeHeureBL blTH, int tH){
		JComboBox comboTEns = new JComboBox();
		for (Record typeEns : blTH.getListeTypeHeure()) {
			//On va selectionner le statut qui correspond au statut de la base
			if(typeEns.getId() == idTHeure){
				comboTEns.addItem(typeEns);
				comboTEns.setSelectedItem(typeEns);
			} else {
				comboTEns.addItem(typeEns);
			}
		}
		return comboTEns;
	}
	
	private JComboBox makeComboProf (EnseignantBL blEns, int tProf){
		JComboBox comboTEns = new JComboBox();
		for (Record typeEns : blEns.getListeEnseignant()) {
			//On va selectionner le statut qui correspond au statut de la base
			if(typeEns.getId() == idProf){
				comboTEns.addItem(typeEns);
				comboTEns.setSelectedItem(typeEns);
			} else {
				comboTEns.addItem(typeEns);
			}
		}
		return comboTEns;
	}
	
	private JComboBox makeComboGroupe (GroupeBL blTH, int tH){
		JComboBox comboTEns = new JComboBox();
		for (Record typeEns : blTH.getListeGroupe()) {
			//On va selectionner le statut qui correspond au statut de la base
			if(typeEns.getId() == idTHeure){
				comboTEns.addItem(typeEns);
				comboTEns.setSelectedItem(typeEns);
			} else {
				comboTEns.addItem(typeEns);
			}
		}
		return comboTEns;
	}

	
	public void initGUI (int idService) {
		TypeHeureBL blTH = new TypeHeureBL();
		EnseignantBL blEns = new EnseignantBL();
		GroupeBL blGr = new GroupeBL();
		
        //si l'id du noeud envoyé est -1, on est en mode ajout, les champs seront donc vides
      
		if (currentViewMode != ViewMode.AJOUT) {
        	ServiceBL serviceBL = new ServiceBL(idService);
			nbPeriode = serviceBL.getNbPeriodes();
        	blTH = new TypeHeureBL(serviceBL.getFk_service_type_heure());
        	idTHeure = blTH.getIdTypeHeure();
        	blEns = new EnseignantBL(serviceBL.getFk_service_enseignant());
        	idProf = blEns.getIdEnseignant();
        	blGr = new GroupeBL(serviceBL.getFk_service_groupe());
        	idGroupe = blGr.getIdGroupe();

        }

		
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());

        JLabel l = new JLabel("Enseignant: ", JLabel.TRAILING);
        p.add(l);
        comboProf = makeComboProf(blEns, idProf);
        componentList.add(comboProf);
        
        l.setLabelFor(comboProf);
        p.add(comboProf);
        
        l = new JLabel("Groupe: ", JLabel.TRAILING);
        p.add(l);
        comboGroupe = makeComboGroupe(blGr, idGroupe);
        componentList.add(comboGroupe);
        		
        l.setLabelFor(comboGroupe);
        p.add(comboGroupe);

        l = new JLabel("Nombre de périodes: ", JLabel.TRAILING);
        p.add(l);
        SpinnerModel sm = new SpinnerNumberModel(nbPeriode, 0, 200, 1);
        spinNbPeriode = new JSpinner(sm);
        componentList.add(spinNbPeriode);
        
        l.setLabelFor(spinNbPeriode);
        p.add(spinNbPeriode);

        l = new JLabel("Type d'heures: ", JLabel.TRAILING);
        p.add(l);
        comboTE = makeComboTEns(blTH, idTHeure);
        componentList.add(comboTE);
        
        l.setLabelFor(comboTE);
        p.add(comboTE);

        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        4, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        add(p, BorderLayout.NORTH);
	    
        if (currentViewMode != ViewMode.CONSULTATION) {
        	// Mode modification - afficher les boutons
			showCommandBox();
        }
        else{
        	componentList.disableAll();
        }
        
	}
	
	private void showCommandBox() {
		JPanel commandBox = new JPanel();
		JButton save = new JButton("Enregistrer");
		save.setActionCommand("Enregistrer");
		save.addActionListener(new ServiceListener());
		commandBox.add(save);
		drawCommandBox(commandBox);
	}
	
	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		if (currentViewMode == ViewMode.CONSULTATION)
			return "Consultation d'un service";
		else if (currentViewMode == ViewMode.AJOUT)
			return "Ajout d'un service";
		else
			return "Modification d'un service";
	}

	@Override
	public boolean hasBeenModified() {
		// TODO Auto-generated method stub
		return (currentViewMode != ViewMode.CONSULTATION);
	}

	//inner class chargée de gérer l'écoute du bouton d'enregistrement du formulaire
	private class ServiceListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getActionCommand().equals("Enregistrer")){
				System.out.println("Demande d'enregistrement");
				ServiceBL serviceSave = new ServiceBL();
				//si -1, c'est un ajout d'enseignant, sinon modification de l'enseignant de l'id concerné
				if(currentViewMode == ViewMode.AJOUT){
						serviceSave.ajouterService(getID(), getNbPeriode(), idSee, getTypeHeure(), getEnseignant(), getGroupe());
						componentList.disableAll();
				} else {
						serviceSave.modifierService(getID(), getNbPeriode(), idSee, getTypeHeure(), getEnseignant(), getGroupe());
						componentList.disableAll();
				}
				creator.refresh();
			}	
		}
	}
	
	//accesseurs
	public int getID() {
		return id;
	}
	
	public int getTypeHeure() {
		return ((Record)comboTE.getSelectedItem()).getId();
	}

	public int getGroupe() {
		return ((Record)comboGroupe.getSelectedItem()).getId();
	}

	public int getEnseignant() {
		return ((Record)comboProf.getSelectedItem()).getId();
	}

	public int getNbPeriode() {
		return Integer.parseInt(spinNbPeriode.getValue().toString());
	}
}
